<?php

namespace App\Http\Controllers\Admin;

use App\Http\Requests\FangRequest;
use App\Models\City;
use App\Models\Fang;
use App\Models\Fangattr;
use App\Models\Fangowner;
use App\Models\Fangrent;
use App\Models\Renting;
use Illuminate\Http\Request;
use GuzzleHttp\Client;
class FangController extends BaseController
{
    public function index(Fang $fang)
    {
        /**$url='https://fang.1314000.cn/admin/login';
        //请求的超时时间，单位，秒
        //verity 是否检测证书，false,不检查
        $client=new Client(['tiemout'=>5,'verify'=>false]);
        //发起请求
        $response=$client->get($url);
        //获取响应体中的数据
        $body=(string)$response->getBody();
        return $body;*/

        //分页显示房源信息
        $data=$fang->paginate($this->pagesize);
        return view('admin.fang.index',compact('fang','data'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //房源属性信息的读取
        $attrData = Fangattr::all()->toArray();
        $attrData = subTreeName($attrData);
        //房东信息读取
        $ownerData = Fangowner::all();
        //读取省份信息
        $pData = City::where('pid', 1)->get()->toArray();
        return view('admin.fang.create', compact('attrData', 'ownerData', 'pData'));
    }

    //添加数据处理 依赖注入
    public function store(FangRequest $request)
    {
        //获取数据
        $data=$request->except(['_token','file']);
        //添加入库之前，得到经纬度转换值，用observer监听
        //入库
        Fang::create($data);
        return redirect(route('admin.fang.index'))->with('success','添加房源成功');
    }

    /**
     * Display the specified resource.
     *
     * @param \App\Models\Fang $fang
     * @return \Illuminate\Http\Response
     */
    public function show(Fang $fang)
    {
        //
    }

    //房源修改显示
    public function edit(Fang $fang)
    {
        //房源属性信息的读取
        $attrData=Fangattr::all()->toArray();
        $attrData=subTreeName($attrData);
        //业主信息读取
        $ownerData=Fangowner::all();
        //省份
        $pData=City::where('pid',1)->get()->toArray();
        //市
        $cData=City::where('pid',$fang->fang_province)->get()->toArray();
        //区
        $rData=City::where('pid',$fang->fang_region)->get()->toArray();
        //指定blade模版显示
        return view('admin.fang.edit',compact('fang','attrData','ownerData','pData','cData','rData'));
    }

    //房源修改数据接收入库
    public function update(FangRequest $request, Fang $fang)
    {
        $data=$request->except(['_token','_method','file']);
        $fang->update($data);
        return redirect(route('admin.fang.index'))->with('success','修改房源成功');
    }

    //地区联动数据返回、
    public function area(Request $request)
    {
        $pid = $request->get('pid');
        $data = City::where('pid', $pid)->get();
        return $data;
    }

    //图片上传
    public function upfile(Request $request)
    {
        $pic = '/uploads/noimg.jpg';
        //上传的节点名称
        $upnode = $request->get('node', 'article');
        if ($request->hasFile('file')) {
            $file = $request->file('file');
            $filename = $file->store($upnode, 'upfile');
            $pic = '/uploads/' . $filename;
        }
        return ['pic' => $pic];
    }

    //添加出租显示
    public function rent(Request $request,Fang $fang){
        //租客列表
        $data=Renting::all();
        return view('admin.fang.rent',compact('fang','data'));
    }
    //添加出租处理
    public function rentStore(Request $request,Fang $fang){
        if($fang->fang_status==0){
            $data=$request->except(['_token']);
            $data['fang_id']=$fang->id;
            //添加数据入库
            Fangrent::create($data);
            //改变房源状态
            $fang->update(['fang_status'=>1]);
        }
        return redirect(route('admin.fang.index'));
    }
}
